var wishService = require('../../../services/mis/weixin/wishService');
var messageUtil = require('../../../common/utils/messageUtil');

function serviceFunc(req,res,fnName,err,result){
    if (err) {
        logger.error('Failed to ' + fnName + '...!' + err.message);
        res.json(messageUtil.composeFailedMessage(req, err.message));
    } else {
        res.json(messageUtil.composeSucceededMessage(req, null, result));
    }
}

// 心愿列表
exports.getBackofficeWishList = function (req, res) {
    var body = req.body;
    var page = req.params.page;
    var size = req.params.size;

    wishService.getBackofficeWishListService(body, page, size, function (err, results) {
        serviceFunc(req,res,"getBackofficeWishListService",err,results);
    })
};

// 用户心愿列表
exports.getBackofficeUserWishList = function (req, res) {
    var body = req.body;
    var page = req.params.page;
    var size = req.params.size;

    wishService.getBackofficeUserWishListService(body, page, size, function (err, results) {
        serviceFunc(req,res,"getBackofficeUserWishListService",err,results);
    })
};

exports.createWish = function (req, res) {
    wishService.createWishService(req.body, function (err, results) {
        serviceFunc(req,res,"createWishService",err,results);
    })
};

exports.updateWish = function (req, res) {
    var wishId = req.params.wishId;

    wishService.updateWishService(wishId, req.body, function (err, results) {
        serviceFunc(req,res,"updateWishService",err,results);
    })
};

exports.updateUserWish = function (req, res) {
    var wishId = req.params.wishId;

    wishService.updateUserWishService(wishId, req.body, function (err, results) {
        serviceFunc(req,res,"updateUserWishService",err,results);
    })
};

exports.deleteWish = function (req, res) {
    var delObj = JSON.parse(req.body.delObj);
    if (typeof delObj == 'object' && delObj.length) {  // an array
        delObj = {
            $in: delObj
        }
    }
    wishService.deleteWishService(delObj, function (err, results) {
        serviceFunc(req,res,"deleteWishService",err,results);
    })
};